// run: $exec < input
#include <iostream>
#include <algorithm>
#include <vector>

int const maxn = 10000000;
bool not_prime[maxn];
int n, d;

bool prime(int x)
{
	return !not_prime[x];
}

int reverse(int n, int d)
{
	std::vector<int> v;
	int tn = n;
	for (; tn; tn /= d) v.push_back(tn % d);
	std::reverse(v.begin(), v.end());
	int ret = 0, base = 1;
	for (int i = 0; i < (int)v.size(); i++) {
		ret += base * v[i];
		base *= d;
	}
	return ret;
}

void init()
{
	not_prime[1] = true;
	for (int i = 2; i < maxn; i++) {
		if (not_prime[i]) continue;
		for (int j = i + i; j < maxn; j += i)
			not_prime[j] = true;
	}
}

int main()
{
	init();
	while (std::cin >> n >> d && n >= 0) {
		if (prime(n) && prime(reverse(n, d)))
			std::cout << "Yes\n";
		else
			std::cout << "No\n";
	}
}

